package com.java.flowable.myflowable.service.impl;


import com.java.flowable.myflowable.exception.ServiceException;
import com.java.flowable.myflowable.service.IFlowInstanceService;
import com.java.flowable.myflowable.factory.FlowServiceFactory;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service

public class FlowInstanceServiceImpl extends FlowServiceFactory implements IFlowInstanceService {

    private final Logger log = LoggerFactory.getLogger(FlowInstanceServiceImpl.class);

    /**
     * 根据流程定义ID启动流程实例
     *
     * @param procDefId 流程定义Id
     * @param variables 流程变量
     * @return
     */
    @Override
    public String startProcessInstanceById(String procDefId, Map<String, Object> variables) {

        try {
            // 设置流程发起人Id到流程中
//            Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
//            identityService.setAuthenticatedUserId(userId.toString());
//            variables.put("initiator",userId);
//            variables.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);
            ProcessInstance processInstance = runtimeService.startProcessInstanceById(procDefId, variables);
            return processInstance.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("--startProcessInstance fail--:{}" + e.getMessage());
        }
    }
}